package com.sky.controller.user;
import com.sky.constant.JwtClaimsConstant;
import com.sky.dto.UserLoginDTO;
import com.sky.entity.User;
import com.sky.properties.JwtProperties;
import com.sky.result.Result;
import com.sky.service.UserService;
import com.sky.utils.JwtUtil;
import com.sky.vo.UserLoginVO;
import io.swagger.annotations.Api;


import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;

/**
 * @Description TODO
 * @Author itheima
 * @Date 2023-07-17 17:28
 */
@RestController
@Slf4j
@Api(tags = "微信登录相关接口")
@RequestMapping("/user/user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private JwtProperties jwtProperties;

    /**
     *
     * @param userLoginDTO
     * @return
     */
    @PostMapping("/login")
    public Result<UserLoginVO> getUserLogin(@RequestBody UserLoginDTO  userLoginDTO){
        log.info("微信登录相关接口;{}",userLoginDTO);
         User user= userService.getUserLogin(userLoginDTO);
         //创建集合存储
        HashMap<String, Object> claims = new HashMap<>();
        //存储 k=名称  v=用户id
        claims.put(JwtClaimsConstant.USER_ID, user.getId());
        //使用工具类生成令牌               //设置jwt签名加密时使用的秘钥   //过期时间
        String token = JwtUtil.createJWT(jwtProperties.getUserSecretKey(), jwtProperties.getUserTtl(), claims);
        UserLoginVO userLoginVO = UserLoginVO.builder()
                .id(user.getId())
                .token(token)
                .openid(user.getOpenid())
                .build();
        return Result.success(userLoginVO);
    }

}
